﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using WmsService.BusinessLogic;
using WmsService.Util;

namespace WmsService.Reports
{
    public class ReportInStoreBillAop : IReportAopHandle
    {
        private const string SqlInvoiceBillID = "select distinct InvoiceNo from dbo.InvoiceBillInStoreBillDtl where InStoreBillDtlID in (select BillDtlID from dbo.InStoreBillDtl where BillID = '{0}')  ";
        #region IReportAopHandle Members

        public void Handle(System.Data.DataSet ds, List<ReportDetail> reportDetails)
        {
            //var factory = LogicFactoryCreator.CreateAbstractFactory();
            
            foreach (var reportDtl in reportDetails)
            {
                if (reportDtl.DbTableName.Equals("InStoreBill", StringComparison.OrdinalIgnoreCase))
                {
                    ds.Tables[reportDtl.DbTableName].Columns.Add("是否增票", typeof(String));
                    ds.Tables[reportDtl.DbTableName].Columns.Add("发票号码", typeof(String));

                    foreach (DataRow row in ds.Tables[reportDtl.DbTableName].Rows)
                    {
                        row["是否增票"] = "";
                        string sql = String.Format(SqlInvoiceBillID, row["BillID"].ToString().Trim());
                        var table = DataAccess.DataQuerier.GetDataTable(sql);
                        string invoiceNos = "";
                        foreach (DataRow dataRow in table.Rows)
                        {
                            invoiceNos += (dataRow["InvoiceNo"].ToString().Trim() + "/");
                        }
                        if (invoiceNos.EndsWith("/"))
                        {
                            invoiceNos = invoiceNos.Substring(0, invoiceNos.Length - 1);
                        }
                        row["发票号码"] = invoiceNos;
                    }
                }
                else if (reportDtl.DbTableName.Equals("InStoreBillDtl", StringComparison.OrdinalIgnoreCase))
                {
                    var table = ds.Tables[reportDtl.DbTableName];
                    
                    table.Columns.Add("名称规格", typeof(String));
                    table.Columns.Add("项目号", typeof(String));
                    table.Columns.Add("请购单号", typeof(String));

                    foreach (DataRow dataRow in ds.Tables[reportDtl.DbTableName].Rows)
                    {
                        dataRow["名称规格"] = dataRow["NewGoodsName"].ToString().Trim() + dataRow["SpecificationType"].ToString();
                    }
                }
            }
        }

        #endregion
    }
}